﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;


namespace DAO
{
    public class HangHoaDAO
    {

        public static bool Sua(HangHoa aCT)
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();
                var query =
    (from ord in rv.HangHoas
     where ord.Id == aCT.Id
     select ord).First();

                query.SoLuongTaiQuay = aCT.SoLuongTaiQuay;
                query.SoLuongTonKho = aCT.SoLuongTonKho;
                

                rv.SubmitChanges();


                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool RutHangTrenQuay(int soluong, string ma)
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();

                rv.RutHang(ma,soluong);
                rv.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        public static HangHoa TimTheoId(int id)
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();

                List<HangHoa> ds = new List<HangHoa>();
                var query =
    (from ord in rv.HangHoas
     where ord.Id == id
     select ord).First();

                
                return query;
            }
            catch
            {
                return null;
            }
        }

        public static List<HangHoa> TimKiem(string MaHH)
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();

                List<HangHoa> ds = new List<HangHoa>();
                var query = from c in rv.HangHoas
                            where c.MaHH.IndexOf(MaHH) >= 0 || c.TenHangHoa.IndexOf(MaHH) >= 0
                            select c;
                ds = query.ToList();

                


                return ds;
            }
            catch
            {
                return null;
            }
        }


        public static List<thongketheospResult> LayDSTheoSP(int id)
        {
            try
            {

                QLSTLinqDataContext rv = new QLSTLinqDataContext();


                List<thongketheospResult> temp = new List<thongketheospResult>();
                var query = rv.thongketheosp(id);


                temp = query.ToList();
                return temp;
            }
            catch
            {
                return null;
            }

        }


        public static List<HangHoa> LayDS()
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();
                List<HangHoa> temp = new List<HangHoa>();
                var query = from c in rv.HangHoas
                            select c;
                temp = query.ToList();
                return temp;
            }
            catch
            {
                return null;
            }
        }

        public static List<thongkengayResult> LayDSTheoNgay(DateTime d)
        {
            try
            {
                
                QLSTLinqDataContext rv = new QLSTLinqDataContext();


                List<thongkengayResult> temp = new List<thongkengayResult>();
                var query = rv.thongkengay(d);


                temp = query.ToList();
                return temp;
            }
            catch
            {
                return null;
            }
            
        }

        public static bool Xoa(int maCT)
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();
                var cust =
    (from c in rv.HangHoas
     where c.Id == maCT
     select c).First();

                rv.HangHoas.DeleteOnSubmit(cust);
                rv.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Them(HangHoa aCT)
        {
            try
            {
                QLSTLinqDataContext rv = new QLSTLinqDataContext();
                rv.HangHoas.InsertOnSubmit(aCT);
                rv.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
